<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<!--main content example start-->
<div id="main">

<jsp:useBean id="customer" scope="session" class="login.SQLLoginBean" />
				
<% 	
	String loginAttribute = "";
	try
	{
		loginAttribute = session.getAttribute("login").toString(); 
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	finally
	{
		
	}
	
	if (!loginAttribute.equals("true"))  //loginAttribute.equals("") || 
	{
		//display regular form coding

		//use the top request if using a graphic as the button, instead of a submit button
		//if (request.getParameter("send.x") != null)
		//use submit method if 
		
	
		//String submitParameter = request.getParameter("formbutton");
		
		
		if (request.getParameter("formbutton") != null)
		//if (request.getParameter("username") != null)
		{
			//grab the information submitted through the form
			String webuser = request.getParameter("username");
			String webpass = request.getParameter("password");

			customer.setUser(webuser);
			customer.setPassword(webpass);
			
			//check to see if the parameters are empty or not
			if ((webuser != null) && (webuser.length() > 0))
			{
				if ((webpass != null) && (webpass.length() > 0))
				{
					//getCustomerId method returns "" if the username/password
					//don't match, and returns the customer's ID if it does.
					String customerIdResult = customer.getBeanCustomerId();
					//if the customerId returned is not empty
					//this might not work, try the 'isEmpty' suggested by Jon
					//if (!customerIdResult.equals(""))
					if (!customerIdResult.isEmpty())
					{
						//create a session for login that's true.
						session.setAttribute("login", "true");
						//create a session for id which contains the customerId
						session.setAttribute("customerid", customerIdResult);
						

							//redirect to the rest of the website with the user logged in
							//this web page seems to be indicating why:
							//  http://www.coderanch.com/t/453816/JSP/java/response-sendRedirect-not-working
							response.sendRedirect("/TravelExpertsWeb/loginsuccess.jsp");
							//return;
							//do the response.sendRedirect which isn't working via javascript:
%>
							<script type="text/javascript">
							window.location = "http://localhost:8080/TravelExpertsWeb/loginsuccess.jsp"
							</script>
<%					}
					else
					{
%>
					<h2>Login Failed</h2>
		 			<h3>That Username and Password combination was not found. Please try again.</h3><br /><br /><br />
<%
					}
				}
				else
				{
					out.println("<h1>Password must be entered.</h1>");
				}
			}
			else
			{
				out.println("<h1>Login name must be entered.</h1>");
			}
		}
%>
		<div id="loginarea">
			<h2>Login</h2>
			
				<h3>If you already have an account, login below:</h3>
				<form name="loginform" method="post" action="">		
					<p>			
					<label>Login Name</label>
					<input name="username" id="username" type="text" size="30" />
					<label>Password</label>
					<input name="password" id="password" type="password" size="30" />
					<br/>
					<br/>
					<input name="formbutton" class="button" type="submit" value="Login"/>		
					</p>		
				</form>	
						
				<br />	
			
				<div id="registerarea">
					<form action="#">	
						<h3>No account? <a href="register.jsp">Register With Us</a></h3>
					</form>	
				</div>
			</div>


<% 	}
	else if (loginAttribute.equals("true"))
	{
		//display something saying the customer is already logged in
%>

			<div id="loginarea">
				<h2>Login</h2>
				<h3>You're already logged in.</h3>
			</div>
			<br />
<%
	}
%>	
		</div>
<!-- main ends -->			